package com.huyanqi.front.rest.resource.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.huyanqi.core.RetdObj;
import com.huyanqi.entity.Share;
import com.huyanqi.front.rest.RespCode;
import com.huyanqi.front.rest.resource.ShareListResource;
import com.huyanqi.front.rest.resp.ShareListResp;
import com.huyanqi.front.rest.resp.ShareSimpleResp;
import com.huyanqi.front.rest.service.ShareRestService;

public class ShareResourceImpl implements ShareListResource{

	private ShareRestService restService;
	
	@Resource(name="ShareRestService")
	public void setRestService(ShareRestService restService) {
		this.restService = restService;
	}
	public ShareRestService getRestService() {
		return restService;
	}
	
	@Override
	public ShareListResp findLatestShare(Integer pageSize, Integer pageNum) {
		ShareListResp slp = new ShareListResp();
		try{
			RetdObj<List<Share>> retd = restService.findLatestShare(pageSize,pageNum);
			slp.setCode(RespCode.valueOf(retd.getCode()));
			slp.setData(retd.getRealObj());
		}catch (Exception e) {
			e.printStackTrace();
			slp.setCode(RespCode.EX_APP);
		}
		return slp;
	}
	
	@Override
	public ShareListResp findMyShare(Long uid) {
		ShareListResp slp = new ShareListResp();
		try{
			RetdObj<List<Share>> retd = restService.findMyShare(uid);
			slp.setData(retd.getRealObj());
			slp.setCode(RespCode.valueOf(retd.getCode()));
		}catch (Exception e) {
			e.printStackTrace();
			slp.setCode(RespCode.EX_APP);
		}
		return slp;
	}
	
	@Override
	public ShareSimpleResp toShare(HttpServletRequest request) {
		ShareSimpleResp ssr = new ShareSimpleResp();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		  SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd/HH");
		  /**构建图片保存的目录**/
		  String logoPathDir = "/images/brand/"+ dateformat.format(new Date());
		  /**得到图片保存目录的真实路径**/
		  String logoRealPathDir = request.getSession().getServletContext().getRealPath(logoPathDir);
		  /**根据真实路径创建目录**/
		  File logoSaveFile = new File(logoRealPathDir);
		  if(!logoSaveFile.exists())
		   logoSaveFile.mkdirs();  
		  /**页面控件的文件流**/
		  MultipartFile multipartFile = multipartRequest.getFile("file"); 
		  /**获取文件的后缀**/
		  String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
		  /**使用UUID生成文件名称**/
		  String logImageName = UUID.randomUUID().toString()+ suffix;//构建文件名称
		  /**拼成完整的文件保存路径加文件**/
		        String fileName = logoRealPathDir + File.separator   + logImageName;          
		        File file = new File(fileName);    
		  try {
		   multipartFile.transferTo(file);
		  } catch (IllegalStateException e) {
		   e.printStackTrace();
		  } catch (IOException e) {  
		   e.printStackTrace();
		  }
		System.out.println(logoPathDir+"/"+logImageName);  
		return ssr;
	}
	
}
